export default {
    methods: {
        // 选择图片并生成本地路径
        async handleChooseImage() {
            if (this.locImages.length < 9) {
                let chooseImg = await this.$native.chooseImage();
                const newImgs = chooseImg.map(item => {
                    return {
                        local: item
                    } //local 本地路径
                })
                const images = [...this.locImages, ...newImgs]
                images.length <= 9 ?
                    (this.locImages = images) :
                    (this.locImages = images.slice(0, 9));
            } else {
                this.$tip.toast("最多上传九张图片");
            }
        },
        // 上传图片并生成绝对路径
        async handleUploadImage(localImageArr, name, data) {
            let arr = []
            let isSaveArr = [] //存草稿转换后的url
            for (let path of localImageArr) {
                // 判断本地选的图片还是草稿箱传来已上传过的图片
                if (path.local) {
                    arr.push(this.$native.upload(this.$http.upLoadImg, path.local, name, data))
                } else {
                    isSaveArr.push(path.isSave)
                }
            }

            let uploadImage_arr = await Promise.all(arr)
            let new_arr = uploadImage_arr.map(item => JSON.parse(item.data).url)
            return [...new_arr, ...isSaveArr] //将本地转化后的url 和草稿存的转化后url 拼起来
        },
        // 预览图片
        handleImagePreview(index) {
            const images = this.locImages.map(item => item.local)
            wx.previewImage({
                current: images[index],
                urls: images,
            })
        },
        // 删除图片
        handleRemoveImage(index) {
            this.locImages.splice(index, 1);
        },
    }
}